#include "ecatbusxml.h"
#include <QDebug>


EcatBusXml::EcatBusXml(QString filePath)
{
   // m_file=NULL;
    busFile=filePath;
}
/*
bool EcatBusXml::openXml()
{
    if(m_file==NULL) {
        m_file = new QFile( m_filePath );
        if(m_filePath.isEmpty())
        {
            qDebug()<< "FilePath isEmpty!";
            return false;
        }
        if( !m_file->exists() )
        {
            qDebug() << "xml error:: not exists  : "<< m_filePath;
            return false;
        }
        if( !m_file->open( QFile::ReadWrite | QFile::Text ) )
        {
            qDebug() << "xml error:: m_file open  : "<< m_filePath;
            return false;
        }

        QString errorStr;
        int errorLine=0;
        int errorColumn=0;

        m_doc.clear();
        if( !m_doc.setContent( m_file, false, &errorStr, &errorLine, &errorColumn))
        {
            qDebug() << "xml error : setContent()  "<<errorStr<<errorLine<< errorColumn  ;
            m_file->close();
            return false;
        }
        m_root = m_doc.documentElement();
        //        if(m_root.tagName()!= "Config"){
        //            qDebug()<<"不是程序所用的xml文件格式"<< m_root.tagName() ;
        //            return false;
        //        }else{
        //            qDebug()<<"xml open : "<< m_root.hasChildNodes();
        //        }
    }
    return  m_root.hasChildNodes() ;
}
*/
/*
void EcatBusXml::saveBusDev(QVector<S_EcatDevInfo> &newInfo){
    openXml();
    m_root.clear();
    S_EcatDevInfo eDev;
    foreach(eDev,newInfo){
        if(eDev.devInfo.alias!=0){
            QDomNode nDode;
            addNode(nDode,"alias",eDev.devInfo.alias );
            addNode(nDode,"XMLFile",eDev.devInfo.alias );
            addNode( m_root, "node" );
        }
    }

    closeXml(true);
}
*/
/*
void EcatBusXml::addBusDev( int node, int alias, QString settingFile ){
    openXml();

    if(alias>0 && !aliasMapFile.contains(alias))
    {
        QDomElement pnode = m_root.createElement("node");
        m_root.appendChild( pnode );

        addNode(pnode,"alias", alias);
        addNode(pnode,"XMLFile", settingFile);

        aliasMapFile.insert(alias,filepath);

        qDebug()<<"add BusDev:"<<info.robotId ;
    }else
        qDebug()<<"add BusDev error: alias exist!"<< alias ;

    closeXml(true);
}
*/

void EcatBusXml::delBusDev(int delalias)
{
    bool ok=openXml(busFile, QFile::ReadWrite | QFile::Text);
    if( !ok ){
        qDebug()<<"open xml failed: node= "<<delalias <<busFile;
        return  ;
    }
    QDomNodeList devNodes = m_root.childNodes();
    for(int i=0;i<devNodes.size();i++)
    {
        QDomNode devND =  devNodes.at(i);
        if( devND.nodeName() .indexOf("#comment")>=0){
            continue;
        }
        int alias = 0;
        QString filepath ;

        readXml( findSubNode( devND, "alias", ok ), alias );
        readXml( findSubNode( devND, "XMLFile", ok ), filepath );

        if(ok && alias==delalias)
        {
            m_root.removeChild( devND );
            qDebug()<<"del BusDev:"<<delalias  ;
            break;
        }
    }

    closeXml(true);
}


//如果设备不存在，却有有配置文件， =不可用状态
bool EcatBusXml::readBusConfig( )
{
    bool ok=openXml( busFile, QFile::ReadOnly | QFile::Text );
    if(!ok)
    {
        qDebug()<<"Bus Config: openXml :"<<ok;
    }
    aliasList.clear();
    aliasMapDeviceType.clear();

    QDomNodeList devNodes = m_root.childNodes();
    for(int i=0;i<devNodes.size();i++)
    {
        QDomNode devND =  devNodes.at(i);
        if( devND.nodeName() .indexOf("#comment")>=0){
            continue;
        }
        int alias = 0;
        int  devtype ;
        QString tmpStr;
        QString filepath ;

        readXml( findSubNode( devND, "alias", ok ), alias );
        readXml( findSubNode( devND, "xmlFile", ok ), filepath );
        readXml( findSubNode( devND, "deviceType", ok ), tmpStr );

        if( tmpStr.indexOf("servo",Qt::CaseInsensitive)>=0 )
        {
            devtype=E_EcatDev_Servo;
        }else  if( tmpStr.indexOf("coupler",Qt::CaseInsensitive)>=0 ){
            devtype=E_EcatDev_Coupler;
        }else  if( tmpStr.indexOf("iomodule",Qt::CaseInsensitive)>=0 ){
            devtype=E_EcatDev_IoModule;
        }else{
            devtype=E_EcatDev_New;
        }

        if(ok && alias>0)
        {
            aliasMapFile.insert(alias,filepath);
            aliasMapDeviceType.insert(alias, devtype);
            aliasList.push_back(alias);
        }else{
            qDebug()<<"不可用的总线配置!"<<i<<alias;
        }
    }

    closeXml();
    return ok?true:false;
}

